import React from 'react';
import { inject, observer } from 'mobx-react';
import { WeaErrorPage, WeaTop, WeaTab, WeaRightMenu, WeaNewScroll, WeaFormItem, WeaSearchGroup, WeaLocaleProvider } from 'ecCom';
import { Row, Button, Spin, Form } from 'antd';
import { WeaForm, WeaSwitch } from 'comsMobx';
import { WeaTableNew } from 'comsMobx';

const WeaTable = WeaTableNew.WeaTable;
const ButtonGroup = Button.Group;
import VotingDialog from '../public/votingDialog/index';
import { observable, action, toJS } from 'mobx';
// import { com } from 'weaMeeting';
// import * as weaMeetingD  from 'weaMeeting';
// let { MeetingDialog, MeetingRoomPlan } = com
const getLabel = WeaLocaleProvider.getLabel;
@inject('votingVotingList')
@observer
class VotingList extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
    };
  }
  componentDidMount() {
    const { doInit, form } = this.props.votingVotingList;
    doInit(conditioninfo => form.basic.initFormFields(conditioninfo));
  }

  componentWillReceiveProps(nextProps) {
    if (this.props.location.key !== nextProps.location.key) {
			const { doInit, form } = this.props.votingVotingList;
			form.basic.reset();
      doInit();
    }
  }

  componentWillUnmount() {
	  // 组件卸载时一般清理一些状态
		let { setShowSearchAd, form } = this.props.votingVotingList;
		setShowSearchAd(false);
		form.basic.resetForm();
	}

  render() {
    let { status, setShowSearchAd, doSearch, comsWeaTableStore, form } = this.props.votingVotingList;
    let { showSearchAd, allSessionkey, loading, votingDialogList, votingDialog, votingDialogTitle } = toJS(status);
    let showSearchAdBasic = showSearchAd ? showSearchAd.basic : false;
    const menu = [
      {
        key: '1',
        icon: <i className="icon-coms-search" />,
        content: getLabel(197, '搜索'),
        onClick: key => doSearch(form.basic.getFormParams()),
      },

    ];
    return (
      <div className="votingList" >
        <div style={{ width: '100%' }}>

          <WeaRightMenu ecId={`${this && this.props && this.props.ecId || ''}_WeaRightMenu@9x4fdt`}
            datas={this.getRightMenu()}
            onClick={this.onRightMenuClick.bind(this)}
            collectParams={{
              favname: getLabel(20042, '调查结果'),
              favouritetype: 2,
              objid: 0,
              importantlevel: 1,
              link: 'wui/index.html#/main/voting/votingList',
            }}
          >
            <WeaTop ecId={`${this && this.props && this.props.ecId || ''}_WeaTop@voery6`}
              loading={loading}
              icon={<i className="icon-coms-investigate" />}
              iconBgcolor="#6D3CF7"
              title={getLabel(20042, '调查结果')}
              buttons={[]}
              showDropIcon={true}
              dropMenuDatas={this.getRightMenu()}
              dropMenuProps={{
                collectParams: {
                  favname: getLabel(20042, '调查结果'),
                  favouritetype: 2,
                  objid: 0,
                  importantlevel: 1,
                  link: 'wui/index.html#/main/voting/votingList',
                },
              }}
              onDropMenuClick={this.onRightMenuClick.bind(this)}
            >
              <div style={{ overflow: 'hidden', height: '100%' }}>
                <WeaTab ecId={`${this && this.props && this.props.ecId || ''}_WeaTab@h7mxo8`}
                  buttonsAd={this.getTabButtonsAd()}
                  searchType={['base', 'advanced']}
                  searchsBaseValue={form.basic.getFormParams().subject}
                  setShowSearchAd={bool => setShowSearchAd(bool, 'basic')}// 显示高级搜索
                  hideSearchAd={() => setShowSearchAd(false, 'basic')}// 隐藏高级搜索
                  searchsAd={<Form ecId={`${this && this.props && this.props.ecId || ''}_Form@3si38b`} horizontal>{this.getSearchs(true)}</Form>}// 高级搜索里面的内容
                  showSearchAd={showSearchAdBasic}// 是否显示高级搜索
                  onSearch={(v) => { doSearch({ subject: v }); }}
                  onSearchChange={(v) => {
                    form.basic.updateFields({
                      subject: {
                        value: v,
                      },
                    });
                  }}
                />
                {comsWeaTableStore.basic &&
                  <WeaTable ecId={`${this && this.props && this.props.ecId || ''}_WeaTable@ybllml`}
                    hasOrder={true}
                    needScroll={true}
                    sessionkey={allSessionkey.basic}
                    comsWeaTableStore={comsWeaTableStore.basic}
                    getColumns={this.getColumns}
                  />
                }
              </div>
            </WeaTop>
          </WeaRightMenu>

          {votingDialog && <VotingDialog ecId={`${this && this.props && this.props.ecId || ''}_VotingDialog@hjd1e7`}
            visible={votingDialog}// 显隐受控
            votingDialogList={votingDialogList}
            votingDialogTitle={votingDialogTitle}
            close={this.close}// 关闭回调,

          />}
        </div>
      </div>

    );
  }

  // 右键菜单
  getRightMenu = (isTopBtn = false) => {
    const { status } = this.props.votingVotingList;
    const { rightMenuDatas } = toJS(status);
    let btns = [];
    if (rightMenuDatas && rightMenuDatas.length) {
      rightMenuDatas.map((item) => {
        item.type !== 'BTN_STORE' && item.type !== 'BTN_HELP' && btns.push({
          key: item.type,
          icon: <i className={item.menuIcon} />,
          content: item.menuName,
        });
      });
    }
    return btns;
  }

  // 右键菜单事件绑定
  onRightMenuClick = (key) => {
    const { form, doSearch } = this.props.votingVotingList;
    if (key == 'BTN_SEARCH') { // 搜索
      doSearch(form.basic.getFormParams());
    }
  }

  close = () => {
    let { status, setVotingDialog } = this.props.votingVotingList;
    setVotingDialog(false, '', '');
  }
  getColumns = (e) => {
    let { status, form, setVotingDialog } = this.props.votingVotingList;


    let newColumns = '';
    newColumns = e.map((column) => {
      let newColumn = { ...column };
      if (newColumn.dataIndex == 'subject' || newColumn.dataIndex == 'id') {
        newColumn.render = (text, record, index) => { // 前端元素转义
          let valueSpan = record[`${newColumn.dataIndex}span`] !== undefined ? record[`${newColumn.dataIndex}span`] : record[newColumn.dataIndex];
          return (
            <div className="wea-votinglist-canClick wea-url-name"
              style={{ cursor: 'pointer' }}
              dangerouslySetInnerHTML={{ __html: valueSpan }}
              onClick={() => { setVotingDialog(true, record.id, record.subject); }}
            />
          );
        };
        return newColumn;
      }
        return newColumn;
    });
    return newColumns;
  }


  getSearchs() {
    let { status, form, doSearch } = this.props.votingVotingList;
    let { conditions } = toJS(status);
    let conditioninfo = conditions && conditions.basic ? conditions.basic : [];
    const { isFormInit } = form.basic;
    let group = [];
    isFormInit && conditioninfo.map((c, i) => {
      let items = [];
      c.items.map((field, index) => {
        items.push({
          com: (<WeaFormItem ecId={`${this && this.props && this.props.ecId || ''}_WeaFormItem@n4y0z2@${(field.domkey && field.domkey[0]) ? field.domkey[0] : `n4y0z2${index}`}`}
            label={`${field.label}`}
            labelCol={{ span: `${field.labelcol}` }}
            wrapperCol={{ span: `${field.fieldcol}` }}
          >
            <WeaSwitch ecId={`${this && this.props && this.props.ecId || ''}_WeaSwitch@nhv9bu@${(field.domkey && field.domkey[0]) ? field.domkey[0] : `nhv9bu${index}`}`} fieldConfig={field} form={form.basic} onChange={this.onChange.bind(this)} onEnterSearch={() => doSearch(form.basic.getFormParams())} />
          </WeaFormItem>),
          colSpan: 1,
        });
      });
      group.push(<WeaSearchGroup ecId={`${this && this.props && this.props.ecId || ''}_WeaSearchGroup@uw1xot@${i}`} needTigger={true} title={c.title} showGroup={c.defaultshow} items={items} />);
    });
    return group;
  }

  onChange = () => {

  }

  getTabButtonsAd = () => {
    let { status, setShowSearchAd, form, doSearch } = this.props.votingVotingList;
    return [
      (<Button ecId={`${this && this.props && this.props.ecId || ''}_Button@6t33ib@searchBtn`} type="primary" onClick={() => { doSearch(form.basic.getFormParams()); }}>{getLabel(197, '搜索')}</Button>),
      (<Button ecId={`${this && this.props && this.props.ecId || ''}_Button@fohvfz@resetBtn`} type="ghost" onClick={() => { form.basic.reset(); }}>{getLabel(2022, '重置')}</Button>),
      (<Button ecId={`${this && this.props && this.props.ecId || ''}_Button@y7trbw@closeBtn`} type="ghost" onClick={() => { setShowSearchAd(false, 'basic'); }}>{getLabel(31129, '取消')}</Button>),
    ];
  }
}


export default VotingList;
